package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name ="查询所有角色数据")
    public String findAll(@RequestParam(defaultValue = "1",name = "page") int pageNum,@RequestParam(defaultValue = "10") int pageSize){
//        当前登录人所属角色的id
        PageInfo<Role> pageBean = roleService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageBean);
        return "system/role/role-list";
    }
    @RequestMapping(value = "/toAdd",name ="进入到添加角色的页面")
    public String toAdd(){

        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit",name ="保存角色数据") //新增或修改时都会进入这个方法
    public String edit(Role role){
        if(StringUtils.isEmpty(role.getId())){
            //id为空新增 save
            role.setId(UUID.randomUUID().toString());
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            role.setCreateTime(new Date());
            roleService.save(role);
        }else{
            role.setUpdateTime(new Date());
            roleService.update(role); //id如果有值 update
        }
//        不管新增还是修改都是要重定向到列表页面
        return "redirect:/system/role/list.do";  //redirect 重定向  在浏览器发起的请求
    }

    @RequestMapping(value = "/toUpdate",name ="进入到编辑页面")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-add";
    }
    @RequestMapping(value = "/delete",name ="根据id删除角色数据")
    public String delete(String id){
        roleService.delete(id);

        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule",name ="进入给角色分配菜单权限页面")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";
    }


  /*  [
    { id:11, pId:1, name:"随意勾选 1-1"},
    { id:111, pId:11, name:"随意勾选 1-1-1"},
    { id:112, pId:11, name:"随意勾选 1-1-2"},
    { id:12, pId:1, name:"随意勾选 1-2"}
    ]*/

    @RequestMapping(value = "/getZtreeNodes",name ="获取树形节点数据")
    @ResponseBody  // 转json+把数据返回到浏览器
    public List<Map> getZtreeNodes(String roleid){
//        查询当前角色下的菜单ids
        List<String> roleModuleIdList = roleService.findModuleLisByRoleId(roleid);  // 菜单id的集合

//        树形节点的数据是从哪里来的？  根据企业id查询模块数据
        List<Module> moduleList = moduleService.findModuleLisByCompanyId(getCompanyId());
//        重新构建一个集合
        List<Map> ztreeNodes = new ArrayList<Map>();
        Map map = null;
        for (Module module : moduleList) {
            map = new HashMap<>();
            map.put("id",module.getId());
            map.put("name",module.getName());
            map.put("pId",module.getParentId());
//            判断roleModuleIdList是否包含 module.getId()
            if(roleModuleIdList.contains(module.getId())){
                map.put("checked",true);    //判断当前角色下的菜单权限是否包含当前的菜单权限
            }
            ztreeNodes.add(map);
        }
        return ztreeNodes;
    }



    @RequestMapping(value = "/updateRoleModule",name ="给角色分配菜单的方法")
    public String updateRoleModule(String roleid,String moduleIds){  //moduleIds="2,201"
        roleService.updateRoleModule(roleid,moduleIds);  // 从数据的本质上 就是向中间表插入数据
        return "redirect:/system/role/list.do";  //完成菜单权限分配后 重定向到角色的列表页面
    }

}
